public class SerchPhoneImg {

    public static final int ALL_IMAGES_FOLDER = -1;//全部图片
 
    //查询媒体库所需的查询参数
    public static final Uri SCAN_IMAGE_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    private static final String[] PROJECTION = {
            MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.MIME_TYPE,
            MediaStore.Images.Media.BUCKET_ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATE_TAKEN
    };
    private static final String SELECTION = MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?";
    private static final String[] SELECTION_ARGS = new String[]{"image/jpeg", "image/png", "image/gif"};
    private static final String ORDER = MediaStore.Images.Media.DATE_TAKEN + " desc";
    private Context mContext;
    private ArrayList<ImageObject> mImageObjectList;
    private SerchFinishCallBack serchFinishCallBack;


    public SerchPhoneImg(Context context, SerchFinishCallBack serchFinishCallBack) {
        this.mContext = context;
        this.serchFinishCallBack = serchFinishCallBack;
        this.mImageObjectList = new ArrayList<>();
    }

    //扫描媒体库中的图片文件
    @WorkerThread
    public void start() {
        ContentResolver contentResolver = mContext.getContentResolver();
        Cursor cursor = contentResolver.query(SCAN_IMAGE_URI, PROJECTION, SELECTION, SELECTION_ARGS, ORDER);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String imagePath = cursor.getString(0);
                String imageMime = cursor.getString(1);
                Integer imageFolderId = cursor.getInt(2);
                String imageFolderName = cursor.getString(3);
                long imageDateToken = cursor.getLong(4);

                ImageObject imageObject = new ImageObject();
                imageObject.setImagePath(imagePath);
                imageObject.setImageMime(imageMime);
                imageObject.setImageFolderId(imageFolderId);
                imageObject.setImageFolderName(imageFolderName);
                imageObject.setImageDateToken(imageDateToken);
                mImageObjectList.add(imageObject);
            }
            cursor.close();
            List<ImageFolder> imageFolderList = getMediaFolder(mImageObjectList);
            try{
                if (imageFolderList.size() > 0) {
                    serchFinishCallBack.serchSuccess(imageFolderList);
                } else {
                    serchFinishCallBack.serchFail();
                }
             }catch (Exception e){
                    serchFinishCallBack.serchFail();
             }   
        }
    }



    //根据图片所在文件夹名称聚类图片
    @WorkerThread
    private List<ImageFolder> getMediaFolder(ArrayList<ImageObject> imageObjectList) {
        //按图片所在文件夹Id进行聚类
        int size = imageObjectList.size();
        Map<Integer, ImageFolder> mediaFolderMap = new HashMap<>();
        //添加全部图片的文件夹
        ImageFolder allImageFolder = new ImageFolder(ALL_IMAGES_FOLDER, mContext.getString(R.string.all_images), imageObjectList.get(0).getImagePath(), imageObjectList);
        mediaFolderMap.put(ALL_IMAGES_FOLDER, allImageFolder);
        //添加其他的图片文件夹
        for (int i = 0; i < size; i++) {
            int imageFolderId = imageObjectList.get(i).getImageFolderId();
            ImageFolder imageFolder = mediaFolderMap.get(imageFolderId);
            if (imageFolder == null) {
                imageFolder = new ImageFolder(imageFolderId, imageObjectList.get(i).getImageFolderName(), imageObjectList.get(i).getImagePath(), new ArrayList<imageObject>());
            }
            ArrayList<imageObject> imageList = imageFolder.getimageObjectList();
            imageList.add(imageObjectList.get(i));
            imageFolder.setImageObjectList(imageList);
            mediaFolderMap.put(imageFolderId, imageFolder);
        }

        //整理聚类数据
        List<ImageFolder> imageFolderList = new ArrayList<ImageFolder>();
        for (Integer folderId : mediaFolderMap.keySet()) {
            imageFolderList.add(mediaFolderMap.get(folderId));
        }
        return imageFolderList;
    }
	
	
	public interface SerchFinishCallBack {
		void serchSuccess(List<ImageFolder> imageFolderList);
		void serchFail();
	}

}
